const path = require('path');
const { BrowserWindow, Notification } = require('electron');
const { getMainWindow } = require('ee-core/electron');
const { isDev, isProd, getBaseDir } = require('ee-core/ps');
const { getConfig } = require('ee-core/config');
const { isFileProtocol } = require('ee-core/utils');
const { logger } = require('ee-core/log');

class OsService {

    constructor(){
        this.windows = {}
    }

    /**
     * 创建窗口
     * @param {*} args
     */
    createWin(args){
        const { type, content, windowName, windowTitle,webRootUrl } = args;
        let contentUrl = null;
        if (type == 'html') {
            contentUrl = path.join('file://', getBaseDir(), content)
        } else if (type == 'web') {
            contentUrl = content;
        } else if (type == 'vue') {
            let addr =  webRootUrl || 'http://localhost:8082'
            if (isProd()) {
                const { mainServer } = getConfig();
                if (isFileProtocol(mainServer.protocol)) {
                    addr = mainServer.protocol + path.join(getBaseDir(), mainServer.indexPath);
                } else {
                    addr = mainServer.protocol + mainServer.host + ':' + mainServer.port;
                }
            }

            contentUrl = addr + content;
        } else {
            // some
        }

        logger.info('[createWindow] url: ', contentUrl);
        const opt = {
            title: windowTitle,
            // x: 10,
            // y: 10,
            width: 980,
            height: 650,
            webPreferences: {
                contextIsolation: false,
                nodeIntegration: true,
            },
        }
        const win = new BrowserWindow({
            autoHideMenuBar: true,
            ...opt
        });
        const winContentsId = win.webContents.id;
        win.loadURL(contentUrl);
        if (isDev()) {
            win.webContents.openDevTools();
        }

        this.windows[windowName] = win;

        return winContentsId;
    }
}

OsService.toString = () => '[class OsService]'

module.exports = {
    OsService,
    osService: new OsService()
}